﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.OleDb;
using System.IO.Ports;
using WFNetLib;

namespace 自消费
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        List<TaskThread> TaskThreadList;
        private void 配置设备ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            配置设备 f = new 配置设备();
            f.ShowDialog();
            FlashDevice();
        }
        void FlashDevice()
        {
            string sql = "select * from device where enabled=true";
            OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql);
            listView1.Items.Clear();
            while(reader.Read())
            {
                deviceData d=deviceDataFactory.Construct(reader);
                ListViewItem lvi = listView1.Items.Add(d.seqnum.ToString());
                lvi.SubItems.Add("COM" + d.commport.ToString());
                lvi.SubItems.Add("");
                lvi.SubItems.Add("");
                lvi.SubItems.Add("");
                lvi.SubItems.Add(d.imei);
                lvi.SubItems.Add("0");
                lvi.SubItems.Add("0");
                lvi.SubItems.Add("0/0");
                lvi.SubItems.Add("");
                lvi.Tag = d.ID;
            }
            reader.Close();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            TaskThreadList = new List<TaskThread>();
            TaskThread.TaskThreadEvent+=new WFNetLib.intstrEventHandler(TaskThread_TaskThreadEvent);
            TaskThread.TaskGroup = 1;
            FlashDevice();
        }
        void TaskThread_TaskThreadEvent(object sender, intstrEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                TaskThread task = (TaskThread)sender;
                switch ((TaskThreadEventType)e.nArg)
                {
                    case TaskThreadEventType.CSQ:
                        if(task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[9].Text = e.strArg;
                        break;
                    case TaskThreadEventType.TaskFail:
                        if(task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[9].Text = e.strArg;                        
                        break;
                    case TaskThreadEventType.NewTask:
                        if (task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[4].Text = e.strArg;
                        break;
                    case TaskThreadEventType.Log:
                        if (task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[2].Text = e.strArg;
                        break;
                    case TaskThreadEventType.Finish:
                        listView1.Items[task.lviIndex].SubItems[2].Text = e.strArg;
                        listView1.Items[task.lviIndex].BackColor = SystemColors.Window;
                        foreach (TaskThread t in TaskThreadList)
                        {
                            if (t.Com.PortName == task.PortName)
                            {
                                TaskThreadList.Remove(t);
                                break;
                            }
                        }
                        break;
                    case TaskThreadEventType.Error:
                        if (task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[2].Text = e.strArg;
                        listView1.Items[task.lviIndex].SubItems[6].Text = (Convert.ToInt32(listView1.Items[task.lviIndex].SubItems[6].Text) + 1).ToString();
                        task.cardTask.fail_reason = e.strArg;
                        task.cardTask.endtime = DateTime.Now;
                        CardTaskDataDBOption.Insert(task.cardTask);
                        break;
                    case TaskThreadEventType.FinishTask:
                        if (task.bClose)
                            break;
                        listView1.Items[task.lviIndex].SubItems[2].Text = e.strArg;
                        listView1.Items[task.lviIndex].SubItems[7].Text = (Convert.ToInt32(listView1.Items[task.lviIndex].SubItems[7].Text) + 1).ToString();
                        task.cardTask.exec_result = e.strArg;
                        task.cardTask.endtime = DateTime.Now;
                        task.cardTask.succeed = true;
                        CardTaskDataDBOption.Insert(task.cardTask);
                        break;
                    case TaskThreadEventType.Stop:
                        listView1.Items[task.lviIndex].SubItems[2].Text = e.strArg;
                        foreach (TaskThread t in TaskThreadList)
                        {
                            if (t.Com.PortName == task.Com.PortName)
                            {
                                try
                                {
                                    listView1.Items[task.lviIndex].BackColor = SystemColors.Window;
                                    TaskThreadList.Remove(t);                                    
                                    break;
                                }
                                catch (System.Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }

                            }
                        }
                        break;
                }
            }));
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripButton1.Text == "启动服务")
            {
                toolStripButton1.Text = "停止服务";
                this.toolStripButton1.Image = global::自消费.Properties.Resources._2;
                foreach (ListViewItem lvi in listView1.Items)
                {
                    string str = lvi.SubItems[1].Text;
                    bool bisgo = false;
                    foreach (TaskThread t in TaskThreadList)
                    {
                        if (t.Com.PortName == str)
                        {
                            bisgo = true;
                            break;
                        }
                    }
                    if (bisgo)
                        continue;
                    OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL("select * from device where id=" + lvi.Tag.ToString());
                    if (reader.Read())
                    {
                        deviceData d = deviceDataFactory.Construct(reader);                        
                        lvi.BackColor = Color.PaleGreen;
                        TaskThread tt = new TaskThread(d, lvi.Index);
                        tt.TaskThreadStart();
                        TaskThreadList.Add(tt);
                        lvi.SubItems[6].Text = "0";
                        lvi.SubItems[7].Text = "0";
                        lvi.SubItems[8].Text = "0/0";
                        lvi.SubItems[9].Text = "";
                        lvi.SubItems[3].Text = "";
                        lvi.SubItems[4].Text = "";
                    }
                    else
                    {
                        MessageBox.Show("数据库出错");
                    }
                    reader.Close();
                }
            }
            else
            {
                toolStripButton1.Text = "启动服务";
                this.toolStripButton1.Image = global::自消费.Properties.Resources._1;
                foreach (ListViewItem lvi in listView1.Items)
                {
                    string str = lvi.SubItems[1].Text;
                    foreach (TaskThread t in TaskThreadList)
                    {
                        if (t.Com.PortName == str)
                        {
                            try
                            {
                                t.Com.Close();
                                lvi.SubItems[2].Text = "等待通道关闭";
                                t.bClose = true;
                                break;
                            }
                            catch (System.Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }

                        }
                    }
                }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            任务设置 f = new 任务设置();
            f.ShowDialog();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                e.Cancel = true;
                return;
            }
            ListViewItem lvi = listView1.SelectedItems[0];
            string str = lvi.SubItems[1].Text;
            bool bisgo=false;
            foreach (TaskThread t in TaskThreadList)
            {
                if (t.Com.PortName == str)
                {
                    bisgo = true;
                    break;
                }
            }
            if (bisgo)
                contextMenuStrip1.Items[0].Text = "停止选中端口";
            else
                contextMenuStrip1.Items[0].Text = "启用选中端口";
        }

        private void 启动选中端口ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem lvi = listView1.SelectedItems[0];
            string str = lvi.SubItems[1].Text;
            if (contextMenuStrip1.Items[0].Text == "停止选中端口")
            {
                foreach (TaskThread t in TaskThreadList)
                {
                    if (t.Com.PortName == str)
                    {
                        try
                        {
                            t.Com.Close();
                            lvi.SubItems[2].Text = "等待通道关闭";
                            t.bClose = true;
                            break;
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        
                    }
                }
            }
            else//启动线程
            {
                OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL("select * from device where id="+lvi.Tag.ToString());
                if (reader.Read())
                {
                    deviceData d = deviceDataFactory.Construct(reader);
                    lvi.BackColor = Color.PaleGreen;
                    TaskThread tt = new TaskThread(d,lvi.Index);                    
                    tt.TaskThreadStart();
                    TaskThreadList.Add(tt);
                    lvi.SubItems[6].Text = "0";
                    lvi.SubItems[7].Text = "0";
                    lvi.SubItems[8].Text = "0/0";
                    lvi.SubItems[9].Text = "";
                    lvi.SubItems[3].Text = "";
                    lvi.SubItems[4].Text = "";
                }
                else
                {
                    MessageBox.Show("数据库出错");
                }
                reader.Close();
            }
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            日志 f = new 日志();
            f.ShowDialog();
        }
    }
}
